package servlet;

import dao.AddressDao;
import dao.OrderDao;
import entity.Address;
import entity.Order;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@WebServlet("/addOrder")
public class AddOrderServlet extends HttpServlet {
	
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

		System.out.println("-----------addOrder------------");
		User user = (User) request.getSession().getAttribute("user");

		Integer goodsId = Integer.valueOf(request.getParameter("goodsId"));
		Integer goodsQty = Integer.valueOf(request.getParameter("goodsQty"));
		String goodsName = request.getParameter("goodsName");
		String goodsImage = request.getParameter("goodsImage");
		String goodsNote = request.getParameter("goodsNote");
		BigDecimal goodsPrice = new BigDecimal(request.getParameter("goodsPrice"));

		System.out.println("selectedGoodsId:"+goodsId);
		System.out.println("selectedGoodsQty:"+goodsQty);
		System.out.println("selectedGoodsNote:"+goodsNote);
		String address = request.getParameter("selectedUserAddress");
		System.out.println("selectedUserAddress:"+address);
		Order order = new Order();
		String orderId = generateOrderNumber();
		System.out.println(orderId);
		Integer state = 0;
		LocalDateTime currentTime = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String createTime = currentTime.format(formatter);
		order.setOrderId(orderId);
		order.setUserId(user.getUserId());
		order.setGoodsId(goodsId);
		order.setGoodsName(goodsName);
		order.setGoodsImage(goodsImage);
		order.setNote(goodsNote);
		order.setPrice(goodsPrice);
		order.setQty(goodsQty);
		order.setOrderSum(goodsPrice.multiply(new BigDecimal(goodsQty)));
		order.setAddress(address);
		order.setState(state);
		order.setCreateTime(createTime);
		order.setUpdateTime(createTime);

		OrderDao dao = new OrderDao();
		int ret = dao.addOrder(order);
		if(ret == -1) {
			response.sendRedirect("error.jsp");
			return;
		}else {
			request.getSession().setAttribute("myNewOrder",order);
			response.sendRedirect("frontend/fin-order.jsp");
		}

	}

	public static String generateOrderNumber() {
		int length = 15;
		StringBuilder sb = new StringBuilder(length);
		Random random = new Random();

		// 可选的字符集合，包含0-9和A-Z
		String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

		for (int i = 0; i < length; i++) {
			// 从字符集合中随机选择一个字符
			char randomChar = characters.charAt(random.nextInt(characters.length()));
			sb.append(randomChar);
		}

		return sb.toString();
	}


}
